import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;

public class StudentForm extends JFrame implements ActionListener {

	DeleteFileManager deleteFileManager;
	UpdateFileManager updateFileManager;
	Student student;
	FileManager fileManager = new FileManager();
	JLabel lblHeader = new JLabel("Student Details");
	JLabel lblname = new JLabel("Student Name");
	JLabel lblSurname = new JLabel("Student Surname");
	JLabel lblstudNo = new JLabel("Student Number");
	JLabel lblCourse = new JLabel("Course");
	JLabel lblDepartment = new JLabel("Department");
	JLabel lblPostalAddress = new JLabel("Postal Address");
	JButton btnSearch = new JButton("Search");
	JButton btnSave = new JButton("Save");
	JButton btnPopulate = new JButton("Populate");
	JButton btnDeleteUpdate = new JButton("Delete and Update");
	JButton btnExit = new JButton("Exit");
	JPanel pnlHeader = new JPanel(new FlowLayout());
	JPanel pnlCenter = new JPanel(new GridLayout(6, 2, 5, 5));
	JPanel pnlBottom = new JPanel(new FlowLayout());
	JTextField txtName = new JTextField();
	JTextField txtSurName = new JTextField();
	JTextField txtStudNumber = new JTextField();
	JComboBox cmbCourse = new JComboBox();
	JComboBox cmbDepartement = new JComboBox();
	JTextField txtAddress = new JTextField();

	public StudentForm() {

		super("Student Form");
		setLayout(new GridLayout(3, 1));
		pnlHeader.add(lblHeader);
		add(pnlHeader);

		pnlCenter.add(lblname);
		pnlCenter.add(txtName);
		txtName.setSize(20, 10);

		pnlCenter.add(lblSurname);
		pnlCenter.add(txtSurName);
		txtName.setSize(20, 10);

		pnlCenter.add(lblstudNo);
		pnlCenter.add(txtStudNumber);
		txtName.setSize(20, 10);

		pnlCenter.add(lblCourse);
		pnlCenter.add(cmbCourse);
		cmbCourse.addItem("IT");
		cmbCourse.addItem("Software Engineering");
		cmbCourse.addItem("Business Fundamentals");
		cmbCourse.setSize(20, 10);

		pnlCenter.add(lblDepartment);
		cmbDepartement.addItem("Enginieering");
		cmbDepartement.addItem("Business");
		cmbDepartement.addItem("Finance");
		pnlCenter.add(cmbDepartement);
		cmbDepartement.setSize(20, 10);

		pnlCenter.add(lblPostalAddress);
		pnlCenter.add(txtAddress);
		txtName.setSize(20, 10);
		add(pnlCenter);

		pnlBottom.add(btnSave);
		pnlBottom.add(btnSearch);
		pnlBottom.add(btnPopulate);
		pnlBottom.add(btnExit);
		pnlBottom.add(btnDeleteUpdate);

		add(pnlBottom);
		btnSave.addActionListener(this);
		btnExit.addActionListener(this);
		btnPopulate.addActionListener(this);
		btnSearch.addActionListener(this);
		btnDeleteUpdate.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Student student;
		if (e.getSource() == btnExit) {
			int option = JOptionPane.showConfirmDialog(null,
					"Are you sure you want to exit");
			if (option == 0) {
				System.exit(1);
			}
		}

		if (e.getSource() == btnSave) {
			student = new Student();
			student.setName(txtName.getText());
			student.setSurname(txtSurName.getText());
			student.setStudNumber(txtStudNumber.getText());
			student.setCourse(cmbCourse.getSelectedItem().toString());
			student.setDepartment(cmbDepartement.getSelectedItem().toString());
			student.setAddress(txtAddress.getText());
			try {
				fileManager.writeToFile(student);
				txtName.setText("");
				txtSurName.setText("");
				txtStudNumber.setText("");
				/*
				 * cmbCourse.removeAllItems(); cmbDepartement.removeAllItems();
				 */
				txtAddress.setText("");
				JOptionPane.showMessageDialog(null, "Record Successfuly Saved");
			} catch (IOException e1) {
				e1.printStackTrace();
			}

		}

		if (e.getSource() == btnPopulate) {
			try {
				ArrayList<Student> students = fileManager.readFromFile();
				for (Student student2 : students) {
					txtName.setText(student2.getName());
					txtSurName.setText(student2.getSurname());
					txtStudNumber.setText(student2.getStudNumber());
					txtAddress.setText(student2.getAddress());
					cmbCourse.setSelectedItem(student2.getCourse());
					cmbDepartement.setSelectedItem(student2.getDepartment());
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		if (e.getSource() == btnSearch) {
			String number = JOptionPane.showInputDialog(null,
					"Please Enter the Student Number");
			try {
				ArrayList<Student> students3 = fileManager.readFromFile();
				for (Student student2 : students3) {
					if (student2.getStudNumber().equals(number)) {
						JOptionPane.showMessageDialog(null, student2);
					} else {
						JOptionPane.showMessageDialog(null,
								"No records were found");
						break;
					}
				}

			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		if (e.getSource() == btnDeleteUpdate) {

			String studentNumber = JOptionPane
					.showInputDialog("Please Enter the Student number");
			try {
				// update a record in the file
				int i = 0;
				student = new Student();
				ArrayList<Student> students3 = fileManager.readFromFile();
				ArrayList<Student> arrayList2 = student.findStudentByNumber(
						studentNumber, students3);
				for (Student student2 : arrayList2)
				{
					student2.setName("Selby");
					students3.set(i, student2);
					updateFileManager = new UpdateFileManager();
					updateFileManager.setStudents(students3);
					Thread thread2 = new Thread(updateFileManager);
					thread2.start();
					i++;
				}
				// delete from a record from the file
                //junior is a guru
				student = new Student();
				ArrayList<Student> students4 = student.deleteStudentByNumber(
						studentNumber, students3);
				deleteFileManager = new DeleteFileManager();
				deleteFileManager.setStudents(students4);
				Thread thread = new Thread(deleteFileManager);
				thread.start();



			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}
}
